<template>
	<view class="contenter">
		<view v-if="plotNum =='' || plotNum == null || plotNum == undefined">
			<view class="choiceCommunity">
				<button type="primary" @click="changeContent()">请选择小区</button>
			</view>
			<view>
				<!-- 普通弹窗 -->
				<uni-popup ref="popup" background-color="#fff" isButton>
					<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }">
						<view class="" style="width: 540rpx;height: 260rpx;background-color: white;">
							<view class="cu-form-group">

								<view class="cu-form-group arrow" @tap="chooseCommunity">
									<text class="cuIcon-home text-green"></text>
									<view class="text-grey">小区</view>
									<input required readonly label="小区" v-model="plotNum" class="text-right"
										placeholder="请选择小区" name="plotNum" icon="arrow"></input>
									<text class='cuIcon-right'></text>
								</view>
							</view>
							<view class="btn" style="display: flex;justify-content: space-around;margin-top: 70rpx;">
								<button style="width: 160rpx;height: 70rpx;line-height: 70rpx;" type="warn"
									@click="cancelCar">取消</button>
								<button style="width: 160rpx;height: 70rpx;line-height: 70rpx;" type="primary"
									@click="ensure">确认</button>
							</view>
						</view>
					</view>
				</uni-popup>
			</view>

		</view>
		<view v-else>
			<view class="home_wrap">
				<!--首页广告-->
				<index-ads></index-ads>
				<index-menu></index-menu>
			</view>
			<index-notice></index-notice>
		</view>
	</view>
</template>

<script>
	import context from '../../lib/java110/Java110Context.js';
	const constant = context.constant;
	import indexAds from '@/components/index/index-ads.vue'
	import indexMenu from '@/components/index/index-menu.vue'
	import indexNotice from '@/components/index/index-notice.vue'
	import {
		getActivitiTitle,
		getCategoryList,
		loadActivites,
		loadAdverts
	} from '../../api/index/indexApi.js'
	import {
		getCommunityId,
		getMallCommunityId,
		getMallCommunityName
	} from '../../api/community/communityApi.js';

	import {
		getCurOwner
	} from '../../api/owner/ownerApi.js'

	import {
		hasLogin
	} from '../../lib/java110/page/Page.js'

	import {
		hasOwner
	} from '../../api/owner/ownerApi.js';

	import conf from '@/conf/config.js'
	export default {
		data() {
			return {
				commonUser: '',
				phone: '',
				plotNum: '',
				isButton: true,
				// show:false,
				userType: '',
				userId: '',
				selectCommunityName: "",
				communityId: '',
				selected: 0,
				mask1Hidden: true,
				mask2Hidden: true,
				animationData: "",
				location: "",
				characteristicSelected: [false, false, false, false, false, false, false],
				discountSelected: null,
				selectedNumb: 0,
				sortSelected: "综合排序",

			};
		},
		components: {
			indexAds,
			indexMenu,
			indexNotice
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			let _that = this;
			uni.setNavigationBarTitle({
				title: conf.systemName
			})
			//查询目录
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {

		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			let that = this;
			let _plotNum = uni.getStorageSync("_selectCommunity");


			let userIdDataPhone = uni.getStorageSync("userInfo");
			that.phone = userIdDataPhone.phone


			// this.plotNum = _plotNum.communityName + "小区";

			if (_plotNum != null && _plotNum != undefined && _plotNum != '') {
				that.plotNum = _plotNum.communityName + "小区";
				that.plotCommunityId = _plotNum.id; //小区id



				let userInfoData = uni.getStorageSync("userInfo"); //微信登录时的用户信息
				
				that.userId = userInfoData.id
				that.userType = userInfoData.proprietorRole
				that.phone = userInfoData.phone
				that.proprietorName = userInfoData.proprietorName
				
				
				let commonUser = uni.getStorageSync("commonUser"); //微信登录时的用户信息
				if (that.plotCommunityId != '' || that.plotCommunityId != 'null' || that.commonUser == '') {
					uni.request({
						url: constant.url.applyOwner, // 成为业主
						header: context.getHeaders(),
						method: "POST",
						data: {
							age: 30,
							avatar: "",
							communityId: that.plotCommunityId,
							id: that.userId,
							identityCard: "",
							name: "微信用户",
							phone: that.phone,
							pke: "暂无门禁",
							proprietorType: 5,
							sex: 0
						},
						success: (resData) => {
							uni.request({
								url: constant.url.intoOwner,
								header: context.getHeaders(),
								method: "GET",
								data: {
									phone: that.phone,
								},
								success: (res) => {
									that.commonUser = res.data.result
									uni.setStorageSync('commonUser', that.commonUser);
									let commonUser = uni.getStorageSync("commonUser"); //调接口保存的有身份id
									that.owerId = commonUser.id
									
									let bindingNum = uni.getStorageSync("bindingState")
									
										if(bindingNum.bindingState != 1){
											uni.request({
												url: constant.url.changCountry,
												header: context.getHeaders(),
												method: "POST",
												data: {
													id: that.owerId,
													phone: that.phone
												},
												success: (res) => {
													that.bindingState = res.data.result
													uni.setStorageSync('bindingState', that.bindingState);
												}
											})
										}
								}
							})
						}
					})
				} else {
					uni.request({
						url: constant.url.intoOwner,
						header: context.getHeaders(),
						method: "GET",
						data: {
							phone: that.phone,
						},
						success: (res) => {
							console.log("返归业主id", res.data)
							that.commonUser = res.data.result
							uni.setStorageSync('commonUser', that.commonUser);
							let commonUser = uni.getStorageSync("commonUser"); //调接口保存的有身份id
							that.owerId = commonUser.id,
								uni.request({
									url: constant.url.changCountry,
									header: context.getHeaders(),
									method: "POST",
									data: {
										id: that.owerId,
										phone: that.phone
									},
									success: (res) => {
										console.log("改小区", res.data)
									}
								})
						}
					})
				}

			}


		},

		methods: {
			// 选择小区
			chooseCommunity: function(e) {
				console.log('点击调用了', e);
				uni.navigateTo({
					url: '/pages/viewCommunitys/viewCommunitys'
				});
			},
			change(e) {
				this.show = e.show
			},
			changeContent(parkNumber) {
				this.type = 'center'
				// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
				this.$refs.popup.open("center")
			},
			cancelCar() {
				this.$refs.popup.close()
			},
			ensure() {
				this.$refs.popup.close()
				uni.setStorageSync('plotNum', this.plotNum);

			},
		}
	};
</script>
<style lang="less" scoped>
	@import "./index.css";

	.contenter {
		width: 100%;
		height: calc(100vh - 0rpx);
		background-color: #191b1f;
		overflow: auto;
	}

	.bock-icon {
		height: 34upx;
		width: 14upx;
		line-height: 100upx;
		background-color: #00AA00;
	}

	.margin-top-1 {
		margin-top: 2upx;
	}

	.tec-height {
		height: 120upx;
	}

	.home_hd {
		position: relative;
		height: 30upx;
		margin-bottom: 20upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.home_wrap {
		background-color: #191b1f;
		padding: 20upx 20upx 0;
	}

	.home_swiper {
		margin-bottom: 20upx;
	}

	.choiceCommunity {
		width: 300rpx;
		height: 100rpx;
		margin: 200rpx auto;
		line-height: 100rpx;

		button {
			width: 100%;
			height: 70rpx;
			text-align: center;
			line-height: 70rpx;
			border-radius: 30rpx;
		}
	}
</style>
